﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace GenericHtml
{
    public class FramePage : System.Web.UI.Page, ILoginInfo
    {
        public UserInfo LoginUser { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            if (this.Request.UrlReferrer == null ||
                string.Compare(this.Request.UrlReferrer.AbsolutePath,
                               "/admin/application.aspx", true) != 0)
            {
                this.WriteParentRediret("/admin/default.aspx");
                return;
            }

            if (!this.Request.IsAuthenticated)
            {
                this.WriteParentRediret("~/admin/login.aspx");
                return;
            }

            this.LoginUser = Session["current_user"] as UserInfo;
            if (this.LoginUser == null)
            {
                // 登录失效 处理
                this.WriteParentRediret("~/admin/login.aspx");
                return;
            }

            
            base.OnLoad(e);
        }

        public void WriteParentRediret(string url)
        {
            this.Response.Write(string.Format(
@"<script type=""text/javascript"">
    if(top != this)
        parent.location.href='{0}';
    else
        document.location.href = '{0}';
</script>",
                    this.ResolveUrl(url)));
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write(
@"<!DOCTYPE html>
<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8""/>
    <title></title>
    <link href=""/css/Site.css"" rel=""stylesheet"" type=""text/css"" />
    <link href=""/css/themes/metro/easyui.css"" rel=""stylesheet"" type=""text/css"" />
    <link href=""/css/themes/icon.css"" rel=""stylesheet"" type=""text/css"" />
    <script src=""/scripts/jquery-1.10.2.min.js"" type=""text/javascript""></script>
    <script src=""/scripts/jquery.easyui.min.js"" type=""text/javascript""></script>
    <script src=""/scripts/easyui-lang-zh_CN.js"" type=""text/javascript""></script>
    <script type=""text/javascript"" src=""/scripts/datagrid-detailview.js""></script>
</head>
<body>");

            base.Render(writer);

            writer.Write(
@"</body>
</html>");
        }
    }
}